package com.bay1ts.service.impl;

import com.bay1ts.dao.*;
import com.bay1ts.domain.*;
import com.bay1ts.service.StudentService;
import com.bay1ts.utils.Term;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by chenuu on 2016/4/1.
 */
@Service("studentService")
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StuSubMapper stuSubMapper;
    @Autowired
    private SubjectContestMapper subjectContestMapper;
    @Autowired
    private RequestMapper RequestMapper;
    @Autowired
    private EntrepreneurshipMapper entrepreneurshipMapper;
    @Autowired
    private VocationaQualificationMapper vocationaQualificationMapper;
    @Autowired
    private UsersMapper usersMapper;
    @Autowired
    private CourseMapper courseMapper;
    @Autowired
    private Class_Mapper classMapper;
    @Autowired
    private ClassScheduleMapper classScheduleMapper;
    @Autowired
    private MajorMapper majorMapper;
    public String addSubjectContest(SubjectContest subjectContest) {
        subjectContestMapper.insert(subjectContest);
        return null;
    }

    public List<SubjectContest> getMySubjectContest() {
        UserDetails userDetails=(UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        Long stu_id=Long.valueOf(userDetails.getUsername());
        List<StuSub> list=stuSubMapper.selectByStudentId(stu_id);
        System.out.println("log --- studentserviceimpl.java getMysubjectcontest method Line:33     list.size()=="+list.size());
        List<SubjectContest> result=new ArrayList<SubjectContest>();
        for(StuSub stuSub:list){
            result.add(getSubjectContestByid(stuSub.getSubId()));
        }
        return result;
    }

    public SubjectContest getSubjectContestByid(int id) {
        return subjectContestMapper.selectByPrimaryKey(id);

    }

    public List<Request> getMyResult(int i) {
        UserDetails userDetails=(UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        Long stu_id=Long.valueOf(userDetails.getUsername());
        List<Request> list=RequestMapper.selectByStudentId(stu_id);
        List<Request> result=new ArrayList<Request>();
        for(Request Request:list){
            if(Request.getStatus()==1){
                //通过的逻辑
                result.add(Request);
            }else{
                //未通过的逻辑
            }
        }
        return result;
    }

    public int getMyAvailableScore() {
        Users users = getUsers();
        return users.getScore();
    }

    public Users getMyself() {
        Users users = getUsers();

        return users;
    }

    public int useScore(int wantToUse){
        Users users = getUsers();
        int i=users.getScore();
        if(wantToUse<= i){
            //说明请求合法，申请的学分比已有的要少，或相等
            //更新学生学分的逻辑
            users.setScore(i-wantToUse);
            usersMapper.updateByPrimaryKey(users);
            //更新学分逻辑结束
            //修改学生-课程 中间表中，该学生该课分数  教务处说 75分
            return 1;
        }else {
            return 0;
        }
    }

    public List<Course> getMyNextCourse() {
        Users users = getUsers();
        int class_id=users.getClaId();
        Class_ myClass=classMapper.selectByPrimaryKey(class_id);
        int grade=myClass.getGrade();
        int term=Term.whichTerm();
        List<Course> courses=new ArrayList<Course>();
        if(term==1){
            //第一学期。本学年的课 就是本学期和下学期
            List<ClassSchedule> classSchedules=classScheduleMapper.selectByClassId(class_id);
            for(ClassSchedule classSchedule:classSchedules){
                Course course=courseMapper.selectByPrimaryKey(classSchedule.getCourseId());
                courses.add(course);
            }
        }else if(term==2){
            //第二学期 本学年下学期的课和下学年上学期的课
            List<ClassSchedule> classSchedules=classScheduleMapper.selectByClassId(class_id);
            for(ClassSchedule classSchedule:classSchedules){
                Course course=courseMapper.selectByPrimaryKey(classSchedule.getCourseId());
                if(course.getTerm()==2){
                    //本学年本学期(2学期)
                    courses.add(course);
                }
            }
            //下学年一学期
            Class_ nextClass=classMapper.selectByMajorIdAndGrade(myClass.getMajId(),grade+1);
            List<ClassSchedule> nextGradeClassSchedules=classScheduleMapper.selectByClassId(nextClass.getId());
            for(ClassSchedule classSchedule:classSchedules){
                Course course=courseMapper.selectByPrimaryKey(classSchedule.getCourseId());
                if(course.getTerm()==1){
                    //下学年1学期(1学期)
                    courses.add(course);
                }
            }
        }else{
            //error
        }
        return courses;
    }

    private Users getUsers() {
        UserDetails userDetails=(UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        Long stu_id=Long.valueOf(userDetails.getUsername());
        return usersMapper.selectByUsername(stu_id);
    }
}
